﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace School.UserCentents
{
    public partial class Centents : System.Web.UI.MasterPage
    {
        #region 传递参数
        /// <summary>
        /// ClassID
        /// </summary>
        public int ClassID
        {
            get
            {
                if (Request.QueryString["classid"] == null || Request.QueryString["classid"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["classid"].ToString());
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.Classes Classes = new BLL.Classes();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["WebSessions"] == null)
            {
                Response.Redirect("/Index.aspx");
            }
            if (!IsPostBack)
            {
                DataSet ds = new DataSet();
                WebSessions m = (WebSessions)Session["WebSessions"];
                lbName.Text = m.UserName;
                Model.Users u = Users.GetModel(m.UserID);

                if (u.UserTypeID <= 2)
                {
                    Panel4.Visible = sldmenu.Visible = xuexiao.Visible = true;
                    xuexiaoshezhi.Visible = false;
                    lbTitle.Text = "管理首页";
                }
                else
                {
                    Panel4.Visible = sldmenu.Visible = false;
                    lbTitle.Text = new BLL.School().GetModel(int.Parse(u.SchoolID.ToString())).Name;
                }

                if (u.UserTypeID == 1)
                {
                    ds = Classes.GetAllList();
                }
                else if (u.UserTypeID == 2)
                {
                    ds = Classes.GetList("Remark1=" + m.UserID);
                }
                else if (u.UserTypeID == 3)
                {
                    Panel4.Visible = sldmenu.Visible = true;
                    xuexiao.Visible = false;

                    ds = Users.GetList("CreateUserID=" + m.UserID);
                    string s = m.UserID + ",";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            s += ds.Tables[0].Rows[i]["ID"].ToString() + ",";
                        }
                    }
                    ds = Classes.GetList("CreateUserID in (" + s.TrimEnd(',') + ") ");
                }
                else if (u.UserTypeID == 4)
                {
                    banji.Visible = true;
                    DataSet ds2 = new BLL.ClassPersonnel().GetList("Manager=" + m.UserID);
                    string s = "";
                    if (ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                        {
                            s += ds2.Tables[0].Rows[i]["ClassID"].ToString() + ",";
                        }
                        ds = Classes.GetList("CreateUserID=" + m.UserID + " or ID in (" + s.TrimEnd(',') + ") ");
                        xiala.Visible = false;
                    }
                }
                Repeater1.DataSource = ds;
                Repeater1.DataBind();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    xiala.Visible = false;
                    Session["ClassID"] = ds.Tables[0].Rows[0]["ID"].ToString();
                }

                if (ClassID > 0)
                {
                    Model.Classes c = Classes.GetModel(ClassID);
                    Session["ClassID"] = ClassID;
                    houtai.InnerText = c.Name;
                    lkClassIndex.PostBackUrl = "MySchool.aspx?id=" + c.ID;
                    lkClassIndex.ToolTip = c.Name;
                }
                else
                {
                    if (Session["ClassID"] != null)
                    {
                        Model.Classes c = Classes.GetModel(int.Parse(Session["ClassID"].ToString()));
                        houtai.InnerText = c.Name;
                        lkClassIndex.PostBackUrl = "MySchool.aspx?id=" + c.ID;
                        lkClassIndex.ToolTip = c.Name;
                    }
                }
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            WebSessions m = (WebSessions)Session["WebSessions"];
            Session.Clear();
            Session.Remove("WebSessions");
            Session.Remove("UserName");
            Session["WebSessions"] = Session["UserName"] = null;
            Session.RemoveAll(); //用于结果了所有的键值
            Session.Abandon(); //用于结果了当前会话
            m.UserID = 0;
            Response.Redirect("../Index.aspx");
        }
    }
}